package com.hurui.xiaoming.activity.creditor;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.BaseActivity;
import com.hurui.xiaoming.adapter.ViewPagerAdapter;
import com.hurui.xiaoming.fragment.creditor.CreditorFragment;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import static com.hurui.xiaoming.R.id.tablayout;

public class CreditorActivity extends BaseActivity {
    /**
     * 催款
     */
    private TabLayout tabLayout ;
    private ViewPager viewPager ;
    private List<Fragment> fragmentlists ;
    private List<String> titlelists ;
    private ViewPagerAdapter adapter ;

    Map map = new HashMap();
    private ImageView search_iv;
    private BottomSheetDialog mBottomSheetDialog;

    private ImageView back_iv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creditor);
        tintManager.setStatusBarTintColor(R.color.systembartintColor2);
        initView();
        initTabs();
        initAction();
    }

    private void initTabs() {
        fragmentlists = new ArrayList<>();

        titlelists    = new ArrayList<>() ;

        titlelists.add("全部");
        titlelists.add("借款中");
        titlelists.add("已还款");
        titlelists.add("已逾期");
        titlelists.add("已结清");

        fragmentlists.add(CreditorFragment.newInstance("0"));
        fragmentlists.add(CreditorFragment.newInstance("1"));
        fragmentlists.add(CreditorFragment.newInstance("3"));
        fragmentlists.add(CreditorFragment.newInstance("4"));
        fragmentlists.add(CreditorFragment.newInstance("5"));

        adapter = new ViewPagerAdapter(getSupportFragmentManager()
                , this , fragmentlists , titlelists);

        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            tabLayout.getTabAt(i).setText(titlelists.get(i));
        }
    }

    @Override
    public void initView() {
        mBottomSheetDialog = new BottomSheetDialog(this);
        search_iv = (ImageView) findViewById(R.id.search_iv);
        tabLayout = (TabLayout) findViewById(tablayout);
        viewPager = (ViewPager) findViewById(R.id.viewpager);

        back_iv = (ImageView) findViewById(R.id.back_iv);
    }

    @Override
    public void initAction() {


        search_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                searchText();
            }
        });

        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WriteReadSharedPrefs.outUser(mContext);
                finish();
            }
        });
    }

    /**
     * 搜索输入
     */

    private void searchText() {

        final View view = LayoutInflater.from(mContext).inflate(R.layout.update_name_address, null, false);
        final EditText myName = (EditText) view.findViewById(R.id.myHobby);
        final TextView textYES = (TextView) view.findViewById(R.id.myOK);

        //点击确认按钮
        textYES.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(myName.getText().toString())) {
                    int selectedTabPosition = tabLayout.getSelectedTabPosition();
                    ((CreditorFragment) fragmentlists.get(selectedTabPosition)).getIntentData(myName.getText().toString(),1);
                }
                mBottomSheetDialog.dismiss();

            }
        });

        createBottomSheetDialog(view);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                showKeyboard(myName);
            }  }, 90);
    }
    /**
     * 底部滑动显示
     *
     */
    private void createBottomSheetDialog( View view) {

        //获取要添加到BottomSheetDialog中的布局view

        mBottomSheetDialog.setContentView(view);
        //显示
        mBottomSheetDialog.show();
    }

    /**
     * 显示软键盘
     *
     */
    public void showKeyboard(EditText editText) {
        if(editText!=null){
            //设置可获得焦点
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            //请求获得焦点
            editText.requestFocus();
            //调用系统输入法
            InputMethodManager inputManager = (InputMethodManager) editText
                    .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(editText, 0);
        }
    }

}
